package com.ebelter.myehc.ui.fragments.modifys;

import android.app.Dialog;
import android.content.DialogInterface;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ebelter.btcomlib.bases.fragment.BaseFragment;

import com.ebelter.btcomlib.common.Constants;

import com.ebelter.btcomlib.models.beans.ScaleUser;
import com.ebelter.btcomlib.models.eventbus.CommonEventBus;
import com.ebelter.btcomlib.models.https.NetUtils;
import com.ebelter.btcomlib.models.https.interfaces.HttpResponse;
import com.ebelter.btcomlib.models.https.responses.BaseResponse;
import com.ebelter.btcomlib.utils.TimeUtils;
import com.ebelter.btcomlib.utils.ToastUtil;
import com.ebelter.btcomlib.utils.UserSpUtil;

import com.ebelter.btcomlib.utils.log.LogUtils;

import com.ebelter.btcomlib.views.dialogs.TextTipDialog;
import com.ebelter.btcomlib.views.dialogs.picktime2.CustomDatePicker;
import com.ebelter.myehc.R;
import com.ebelter.myehc.models.http.request.EhcNetUtils;

import com.ebelter.myehc.ui.activitys.MainActivity;
import com.ebelter.myehc.ui.view.CombinView6;

import org.greenrobot.eventbus.EventBus;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import static com.ebelter.myehc.ui.activitys.LoginActivity.getAgeByBirthday;
import static com.ebelter.myehc.ui.activitys.LoginActivity.getBirthdayIntValue;


public class ModifyBirthday_Fragment extends BaseFragment implements View.OnClickListener {


    public static final String TAG = "ModifyBirthday_Fragment";
    private TextView m_title_tv;
    private TextView m_righttitle_tv;
    private ImageView top_left_iv;

    private CombinView6 birth_cb6;
    String birthday;

    @Override
    protected int setLayoutResouceId() {
        return R.layout.fragment_modify_birthday;
    }

    @Override
    protected void initView() {
        super.initView();
        FV();
        m_title_tv.setText(R.string.chshny);
        m_righttitle_tv.setText(R.string.done);

        birthday = UserSpUtil.readString(Constants.IUser.BIRTHDAY);
        birth_cb6.setTitle2(TimeUtils.formatTime3( TimeUtils.parseFormatter3Time(birthday).getTime()));
    }
    @Override
    protected void setListener() {
        super.setListener();

        m_righttitle_tv.setOnClickListener(this);
        top_left_iv.setOnClickListener(this);
        birth_cb6.setOnClickListener(this);

    }




    private void FV() {
        m_title_tv=mRootView.findViewById(R.id.m_title_tv);
        m_righttitle_tv=mRootView.findViewById(R.id.m_righttitle_tv);
        top_left_iv=mRootView.findViewById(R.id.top_left_iv);

        birth_cb6=mRootView.findViewById(R.id.birth_cb6);


    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.m_righttitle_tv:
                complete ();
                break;
            case R.id.top_left_iv:
                back();
                break;
            case R.id.birth_cb6:
                birth_cb6_Click();
                break;
        }
    }

    //
    private void birth_cb6_Click() {
        birth_cb6.setImage1(R.drawable.ic_shangla);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
        String time = sdf.format(new Date());
        String date = time.split(" ")[0];
        CustomDatePicker datePicker= new CustomDatePicker(getActivity(),new CustomDatePicker.ResultHandler() {
            @Override
            public void handle(String time) {
                LogUtils.i(TAG,time);
                birthday = time.substring(0,10);

            }
        }, "1945-01-01 00:00", time);
        // datePicker.setSelectedCalender("1991-06-13");
        datePicker.showSpecificTime(false); //显示时和分
        datePicker.setIsLoop(false);
        datePicker.setDayIsLoop(true);
        datePicker.setMonIsLoop(true);
        datePicker.show(date);

        datePicker.setSelectedTime(birthday);

        datePicker.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                if (birth_cb6!=null){
                    birth_cb6.setImage1(R.drawable.ic_xiala);
                }
            }
        });
        datePicker.setBackgroundColor(getResources().getColor(R.color.redTheme));
        datePicker.setListener(new TextTipDialog.ICancelOkBtListener() {
            @Override
            public void cancel(Dialog dialogView) {}//内部已经处理了

            @Override
            public void ok(Dialog dialogView) {
               // birth_cb6.setTitle2(birthday);
                if (birth_cb6!=null){birth_cb6.setTitle2(birthday);}

            }
        });


    }

    //修改生日
    private void updateBirthDay(final String time) {

        if (!NetUtils.available()){
            ToastUtil.show(R.string.Network_error);
            return;
        }

        final String selectTime=time.substring(0,10);
        LogUtils.i(TAG,selectTime);


        String authKey = MainActivity.getAuthKey();
        EhcNetUtils.getInstance().updatePersonBirthday(this, authKey, selectTime, new HttpResponse<BaseResponse>() {
            @Override
            public void result(boolean isRequestSuccess, String requestDes, BaseResponse baseResponse, String s) {
                LogUtils.i(TAG,s);
                if (isRequestSuccess&&baseResponse.resultCode==1){
                    //说明修改成功
                    ToastUtil.show(R.string.Modify_Success);

                    UserSpUtil.writeString(Constants.IUser.BIRTHDAY,selectTime);
                    int year=getBirthdayIntValue(selectTime,0,1990);
                    UserSpUtil.writeInt(Constants.IUser.YEAR,year);
                    int month=getBirthdayIntValue(selectTime,1,6);
                    UserSpUtil.writeInt(Constants.IUser.MONTHS,month);
                    int day=getBirthdayIntValue(selectTime,2,1);
                    UserSpUtil.writeInt(Constants.IUser.DAY,day);
                    int age=getAgeByBirthday(year);
                    UserSpUtil.writeInt(Constants.IUser.AGE,age);



                    if (birth_cb6!=null)birth_cb6.setTitle2(TimeUtils.formatTime3(TimeUtils.parseFormatter3Time(time).getTime()));
                    // TODO: 2018/8/28   如果此时连接的是体脂秤 需要下发新的用户信息给秤

                    ScaleUser scaleUser = ScaleUser.getUser();
                    scaleUser.setAge(age);
                    scaleUser.setBirthday(selectTime);

                    CommonEventBus commonEventBus = new CommonEventBus(TAG, MainActivity.TAG,0,"设置了生日-需要重新下发年龄");
                    EventBus.getDefault().post(commonEventBus);

                }else {
                    //说明修改失败
                    ToastUtil.show(R.string.Modify_Failed);
                }


            }
        });
    }


    private void back() {    if (mActivity!=null)mActivity.finish();
    }

    private void complete() {
        updateBirthDay(birthday);
    }
}
